package service

import "time"

type Session struct {
	Id         int64
	CreateTime time.Time
	Userid     int64  // 用户ID
	Token      string // token
	Role       string // 用户角色
}

func AddSession(userid int64, token, role string) (err error) {
	s := Session{
		CreateTime: time.Now(),
		Userid:     userid,
		Token:      token,
		Role:       role,
	}
	_, err = Engine.InsertOne(&s)
	return
}

func GetSessionByToken(token string) (has bool, s Session, err error) {
	has, err = Engine.Where("token = ?", token).Get(&s)
	return
}

func IsLogin(token string) (login bool, err error) {
	has, _, err := GetSessionByToken(token)
	return has, err
}
